//
//  BonusPrototypeParser.cpp
//  airfight
//
//  Created by aaron on 12-11-22.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>
#include "BonusPrototypeParser.h"
#include "Bonus.h"

BonusPrototypeParser::BonusPrototypeParser()
:prototypes(NULL)
,currentBonus(NULL)
{
}

BonusPrototypeParser::~BonusPrototypeParser()
{
    CC_SAFE_RELEASE(currentBonus);
    CC_SAFE_RELEASE(prototypes);
}

BonusPrototypeParser * BonusPrototypeParser::formatWithXMLFile(const char *xmlFile)
{
    BonusPrototypeParser *pRet = new BonusPrototypeParser();
    if(pRet->initWithXMLFile(xmlFile))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

void BonusPrototypeParser::internalInit(const char* tmxFileName)
{
    prototypes = BonusPrototypes::getInstance();
    prototypes->retain();
    if (tmxFileName != NULL)
    {
        m_sXMLFileName = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(tmxFileName);
    }
}

void BonusPrototypeParser::startElement(void *ctx, const char *name, const char **atts)
{
    
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
    if(atts && atts[0])
    {
        for(int i = 0; atts[i]; i += 2)
        {
            std::string key = (char*)atts[i];
            std::string value = (char*)atts[i+1];
            attributeDict->insert(std::pair<std::string, std::string>(key, value));
        }
    }
    if("bonus" == elementName )
    {
        Bonus *bonus = new Bonus();
        bonus->setBonusID((int)atoi(valueForKey("id", attributeDict)));
        bonus->setSpeed((int)atoi(valueForKey("speed", attributeDict)));
        bonus->setType((int)atoi(valueForKey("type", attributeDict)));
        bonus->setRes(valueForKey("res", attributeDict));
        if (!currentBonus)
        {
            currentBonus = bonus;
        }
    }
}

void BonusPrototypeParser::endElement(void *ctx, const char *name)
{
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    if("bonus" == elementName)
    {
        
        prototypes->addBonus(currentBonus);
        currentBonus->release();
        currentBonus = NULL;
    }
   
}

void BonusPrototypeParser::textHandler(void *ctx, const char *s, int len)
{
    
}
